package pl.pw.gis.gc_sa.tests;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.junit.BeforeClass;
import org.junit.Test;

import pl.pw.gis.gc_sa.data.Graph;
import pl.pw.gis.gc_sa.data.VertexChange;
import pl.pw.gis.gc_sa.utils.Utils;

/**
 * Klasa zawierajaca testy wykonywane przy pomocy biblioteki jUnit.
 * 
 * W tej klasie testowana jest poprawnosc wyliczania kosztu kolorowania przez
 * metode naiveArticleCostFunction. Testowanie odbywa sie dla kilku malych
 * grafow, gdzie poprawne wyniki kosztu zostaly wyliczone recznie
 * 
 * @author Lukasz
 * 
 */
public class ArticleNaiveCostFunctionTest {

    static Random random;

    @BeforeClass
    public static void initialize() {
        random = new Random();
    }

    @Test
    public void cost2Graph3Test() throws Exception {
        Graph graph3 = Graph.createVPGraph(3, 1);
        int[] solution = new int[] { 1, 2, 3 };

        assertEquals(-3, Utils.naiveArticleCostFunction(solution, graph3));

        List<VertexChange> changes = new ArrayList<VertexChange>();
        List<Integer> goodAnswers = new ArrayList<Integer>();

        changes.add(changeVertexColor(solution, 2, 1));
        goodAnswers.add(-1);
        changes.add(changeVertexColor(solution, 1, 1));
        goodAnswers.add(9);

        for (int i = 0; i < changes.size(); i++) {
            solution[changes.get(i).getVertexId()] = changes.get(i)
                    .getNewColor();
            assertEquals(goodAnswers.get(i).intValue(),
                    Utils.naiveArticleCostFunction(solution, graph3));

        }
    }

    @Test
    public void cost2Graph3Test2() throws Exception {
        Graph graph3 = Graph.createVPGraph(3, 1);
        int[] solution = new int[] { 0, 1, 2 };

        assertEquals(-3, Utils.naiveArticleCostFunction(solution, graph3));

        List<VertexChange> changes = new ArrayList<VertexChange>();
        List<Integer> goodAnswers = new ArrayList<Integer>();

        changes.add(changeVertexColor(solution, 0, 1));
        goodAnswers.add(-1);
        changes.add(changeVertexColor(solution, 0, 0));
        goodAnswers.add(-3);
        changes.add(changeVertexColor(solution, 1, 2));
        goodAnswers.add(-1);
        changes.add(changeVertexColor(solution, 0, 1));
        goodAnswers.add(-1);

        for (int i = 0; i < changes.size(); i++) {
            solution[changes.get(i).getVertexId()] = changes.get(i)
                    .getNewColor();
            assertEquals(goodAnswers.get(i).intValue(),
                    Utils.naiveArticleCostFunction(solution, graph3));

        }
    }

    @Test
    public void cost2Graph4Test() throws Exception {
        Graph graph4 = Graph.createVPGraph(4, 1);
        int[] solution = new int[] { 1, 2, 3, 4 };

        assertEquals(-4, Utils.naiveArticleCostFunction(solution, graph4));

        List<VertexChange> changes = new ArrayList<VertexChange>();
        List<Integer> goodAnswers = new ArrayList<Integer>();

        changes.add(changeVertexColor(solution, 3, 1));
        goodAnswers.add(-2);
        changes.add(changeVertexColor(solution, 2, 1));
        goodAnswers.add(8);
        changes.add(changeVertexColor(solution, 2, 2));
        goodAnswers.add(0);
        changes.add(changeVertexColor(solution, 3, 4));
        goodAnswers.add(-2);

        for (int i = 0; i < changes.size(); i++) {
            solution[changes.get(i).getVertexId()] = changes.get(i)
                    .getNewColor();
            assertEquals(goodAnswers.get(i).intValue(),
                    Utils.naiveArticleCostFunction(solution, graph4));

        }
    }

    private VertexChange changeVertexColor(int[] solution, int id, int color) {
        int oldColor = solution[id];
        return new VertexChange(id, oldColor, color, true);
    }

}
